import Vue from 'vue' //引入vue js是一个vue的配置文件 vue版本是2.6
import Router from 'vue-router' //vue-router 安装的对象 npm install vue-router@"3.0.1"
import VueRouter from "vue-router";


import Index from "../views/Index" 
import Login from "../views/login/Login.vue"
import Registered from '../views/registered/registered.vue'
//Index childern
import Home from "../views/Index/Home.vue"
import Student from "../views/Student/Studnt.vue"
import Course from "../views/Course/course.vue"
import Leave from "../views/Leave/leave.vue"

// Link children
import Gitee from "../views/link/Gitee.vue"

//vue中使用Router
Vue.use(Router)
const routers =[ //配置router与组件之间的对应关系
    
    {
        path: '/',
        component: Index,
        name: 'index',
        children:[
            {
                path: '/home',
                component: Home,
                name: 'home',
            },
            {
                path: '/student',
                component: Student,
                name: 'student',
            },
            {
                path: '/course',
                component: Course,
                name: 'course',
            },
            {
                path: '/leave',
                component: Leave,
                name: 'leave',
            },
            {
                path: '/gitee',
                component: Gitee,
                name: 'Gitee',
            },
        ]
    },
    {
        path: '/login',
        component: Login,
        name: 'login',
    },
    {
        path: '/registered',
        component: Registered,
        name: 'registered',
    },
]
//创建一个vue-router实例对象
const router =new VueRouter({
    mode:"history",//history 不带#号 hash 带#号
    routes:routers //所有的路由数组
})
router.beforeEach((to,from,next)=>{
    const user = localStorage.getItem("unlogin");
    if (user === "false"){//login
        if(to.name === "login"){
             next("/");
        }  
        else{
            next()
        }
    }
    else{
        next()
    }
})
//导出路由对象  使用路由的地方引入即可  一般在入口main.js中使用
export default router;
